package com.definesys.smecrailtransit.module.LineReport;

import android.databinding.DataBindingUtil;

import com.definesys.smecrailtransit.api.SmecRailConstant;
import com.definesys.smecrailtransit.module.LineReport.bean.LineReportBean;
import com.definesys.smecrailtransit.module.LineReport.viewController.ViewController;
import com.hwangjr.rxbus.annotation.Subscribe;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.base.SmecBaseFragment;
import com.definesys.smecrailtransit.databinding.FragementLineReportBinding;
import com.definesys.smecrailtransit.module.LineReport.adapter.LineReportFaultAdapter;
import com.definesys.smecrailtransit.module.LineReport.adapter.LineReportTimeAdapter;
import com.definesys.smecrailtransit.module.LineReport.bean.FaultStatisticsBean;
import com.definesys.smecrailtransit.module.LineReport.bean.RunTimePerBean;
import com.definesys.smecrailtransit.module.LineReport.viewController.PieChartController;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by cjy on 2018/1/3.
 */

public class LineReportFragment extends SmecBaseFragment<LineReportPresenter> {
    public static final String LINE_REPORT_SUCCESS = "getLineReportSuccess";
    public static final String LINE_REPORT_ERROR = "getLineReportError";
    public static final String JPUSH_LOAD_LINE_REPORT = "";


    private FragementLineReportBinding fragementLineReportBinding;
    private PieChartController pieChartController;
    private ArrayList<FaultStatisticsBean> faultList;
    private ArrayList<RunTimePerBean> timeList;

    private LineReportFaultAdapter lineReportFaultAdapter;
    private LineReportTimeAdapter lineReportTimeAdapter;

    private ViewController controller;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragementLineReportBinding = DataBindingUtil.inflate(inflater, R.layout.fragement_line_report,container,false);
        mPresenter = new LineReportPresenter(fragementLineReportBinding.getRoot().getContext());
        controller = new ViewController();
        fragementLineReportBinding.setViewController(controller);
        _initView();
        //_initData();
        return fragementLineReportBinding.getRoot();
    }

    private void _initData(){
        HashMap<String,Object> hashmap = new HashMap<>();
        hashmap.put("line","12");
        mPresenter.getLineReport(hashmap);
    }

    private void _initView() {
        faultList = new ArrayList<>();
        timeList = new ArrayList<>();

        fragementLineReportBinding.timeListLine.setVisibility(View.GONE);
        fragementLineReportBinding.faultListLine.setVisibility(View.GONE);

        //设置日期

        //绘制饼图
        pieChartController = new PieChartController(fragementLineReportBinding.pieChart, fragementLineReportBinding.getRoot().getContext());
        fragementLineReportBinding.pieChart.setVisibility(View.GONE);


        //故障统计列表
        lineReportFaultAdapter = new LineReportFaultAdapter();
        lineReportFaultAdapter.setList(faultList);
        fragementLineReportBinding.faultList.setLayoutManager(new LinearLayoutManager(getActivity()));
        fragementLineReportBinding.faultList.setNestedScrollingEnabled(false);
        fragementLineReportBinding.faultList.setAdapter(lineReportFaultAdapter);

        //运行时长列表
        lineReportTimeAdapter = new LineReportTimeAdapter(getResources().getDisplayMetrics());
        lineReportTimeAdapter.setList(timeList);
        fragementLineReportBinding.timeList.setLayoutManager(new LinearLayoutManager(getActivity()));
        fragementLineReportBinding.timeList.setNestedScrollingEnabled(false);
        fragementLineReportBinding.timeList.setAdapter(lineReportTimeAdapter);

        //点击事件
        fragementLineReportBinding.faultListImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(fragementLineReportBinding.faultList.getVisibility() == View.VISIBLE){
                    fragementLineReportBinding.faultList.setVisibility(View.GONE);
                    fragementLineReportBinding.faultListLine.setVisibility(View.GONE);
                    fragementLineReportBinding.faultListImg.setImageDrawable(getResources().getDrawable(R.drawable.line_report_top));
                }else{
                    fragementLineReportBinding.faultList.setVisibility(View.VISIBLE);
                    fragementLineReportBinding.faultListLine.setVisibility(View.VISIBLE);
                    fragementLineReportBinding.faultListImg.setImageDrawable(getResources().getDrawable(R.drawable.line_report_bottom));
                }
            }
        });

        fragementLineReportBinding.timeListImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(fragementLineReportBinding.timeList.getVisibility() == View.VISIBLE){
                    fragementLineReportBinding.timeList.setVisibility(View.GONE);
                    fragementLineReportBinding.timeListLine.setVisibility(View.GONE);
                    fragementLineReportBinding.timeListImg.setImageDrawable(getResources().getDrawable(R.drawable.line_report_top));
                }else{
                    fragementLineReportBinding.timeList.setVisibility(View.VISIBLE);
                    fragementLineReportBinding.timeListLine.setVisibility(View.VISIBLE);
                    fragementLineReportBinding.timeListImg.setImageDrawable(getResources().getDrawable(R.drawable.line_report_bottom));
                }
            }
        });

        //设置下拉刷新的属性
        fragementLineReportBinding.refresh.setHeaderView(SmecRailConstant.getSinaRefreshView());
        fragementLineReportBinding.refresh.setAutoLoadMore(true);
        fragementLineReportBinding.refresh.setEnableOverScroll(false);
        fragementLineReportBinding.refresh.setEnableLoadmore(false);
        fragementLineReportBinding.refresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                _initData();
            }
        });

        fragementLineReportBinding.refresh.startRefresh();

    }


    @Override
    protected LineReportPresenter getPersenter() {
        return new LineReportPresenter(this.getContext());
    }

    public static LineReportFragment newInstance(){
        Bundle args = new Bundle();
        LineReportFragment fragment = new LineReportFragment();
        fragment.setArguments(args);
        return fragment;
    }



    /**
     * 获取线路详情-成功
     */
    @Subscribe(
        thread = EventThread.MAIN_THREAD,
        tags = {
                @Tag(LINE_REPORT_SUCCESS)
        }
    )
    public void getLineReportSuccess(LineReportBean data){
        faultList = data.getFaultList();
        timeList = data.getRuntimeList();

        lineReportFaultAdapter.setList(faultList);
        lineReportTimeAdapter.setList(timeList);

        lineReportFaultAdapter.notifyDataSetChanged();
        lineReportTimeAdapter.notifyDataSetChanged();

        fragementLineReportBinding.faultListLine.setVisibility(View.VISIBLE);
        fragementLineReportBinding.timeListLine.setVisibility(View.VISIBLE);

        fragementLineReportBinding.pieChart.setVisibility(View.VISIBLE);
        pieChartController.setData(data.getCurrentFaultCount(),data.getCurrentOfflineCount(),data.getCurrentNormalCount());
        pieChartController.initPieView();

        controller.setFaultCount(data.getFaultEleCount());
        controller.setTimeCount(data.getAbnormalEleCount());

        fragementLineReportBinding.refresh.finishRefreshing();
        fragementLineReportBinding.invalidateAll();

    }

    /**
     * 获取线路详情-失败
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(LINE_REPORT_ERROR)
            }
    )
    public void getLineReportError(Object o){
        fragementLineReportBinding.faultListLine.setVisibility(View.GONE);
        fragementLineReportBinding.timeListLine.setVisibility(View.GONE);

        controller.setFaultCount(0);
        controller.setTimeCount(0);

        fragementLineReportBinding.refresh.finishRefreshing();
        fragementLineReportBinding.pieChart.setVisibility(View.GONE);

        fragementLineReportBinding.invalidateAll();
    }

    /**
     * Jpush跳转
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(JPUSH_LOAD_LINE_REPORT)
            }
    )
    public void jpushLineReport(Object o){
        fragementLineReportBinding.refresh.startRefresh();
    }
}
